package ${entity.packageName}.logic.services;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.gonsin.base.GPageRequest;
import com.gonsin.base.GPageResult;
import com.gonsin.base.exceptions.MyException;
import com.gonsin.gview.annotations.actions.GAction;
import com.gonsin.gview.annotations.actions.GSession;
import com.gonsin.gview.annotations.data.GDataChanged;
import com.gonsin.gview.annotations.data.GDataSource;
import com.gonsin.venus.base.interfaces.enums.BaseResultCodes;
import com.gonsin.venus.base.logic.keyOnly.KeyOnlyUtils;
import com.gonsin.venus.base.logic.utils.PageableUtils;
import ${entity.packageName}.bean.*;
import ${entity.packageName}.interfaces.I${entity.entityName}Service;
import ${entity.packageName}.logic.mapper.*;
import ${entity.packageName}.po.${entity.entityName}Po;
import ${entity.packageName}.po.${entity.entityName}SearchPo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ${entity.entityName}ServiceImpl implements I${entity.entityName}Service {

    @Autowired private ${entity.entityName}Mapper ${entity.entityNameFirstLower}Mapper;

    @GDataSource("search${entity.entityName}")
    @Override
    public GPageResult<${entity.entityName}Bean> search${entity.entityName}(${entity.entityName}SearchPo searchPo, GPageRequest pageRequest) {
        if(StringUtils.isEmpty(pageRequest.getSort())){
            pageRequest.setSort("-create_time");
        }
        List<${entity.entityName}Bean> list = ${entity.entityNameFirstLower}Mapper.search${entity.entityName}(searchPo, PageableUtils.toPageableMyBatis(pageRequest));
        int count = ${entity.entityNameFirstLower}Mapper.count${entity.entityName}(searchPo);
        return GPageResult.result(list, count, pageRequest);
    }

    /**
     * 批量查询${entity.entityLabel}信息
     *
     * @param ${entity.entityNameFirstLower}Keys ${entity.entityLabel}主键KEY列表
     * @return ${entity.entityLabel}信息列表
     */
    @Override
    public List<${entity.entityName}Bean> batchGet${entity.entityName}(List<String> ${entity.entityNameFirstLower}Keys) {
        return ${entity.entityNameFirstLower}Mapper.selectBatchIds(${entity.entityNameFirstLower}Keys);
    }

    /**
     * 主键查询${entity.entityLabel}信息
     *
     * @param ${entity.entityNameFirstLower}Key ${entity.entityLabel}主键KEY
     * @return ${entity.entityLabel}信息
     */
    @GDataSource("get${entity.entityName}")
    @Override
    public ${entity.entityName}Bean get${entity.entityName}(String ${entity.entityNameFirstLower}Key) {
        return ${entity.entityNameFirstLower}Mapper.get${entity.entityName}(${entity.entityNameFirstLower}Key);
    }

    /**
     * 主键删除${entity.entityLabel}信息
     *
     * @param ${entity.entityNameFirstLower}Key ${entity.entityLabel}主键KEY
     */
    @GAction("delete${entity.entityName}ByKey")
    @GDataChanged("search${entity.entityName}")
    @Transactional
    @Override
    public void delete${entity.entityName}(String ${entity.entityNameFirstLower}Key) {
        //删除${entity.entityLabel}
        ${entity.entityNameFirstLower}Mapper.deleteById(${entity.entityNameFirstLower}Key);
    }

    //TODO ${entity.entityLabel}批量删除
    @GAction("batchDelete${entity.entityName}")
    @GDataChanged("search${entity.entityName}")
    @Transactional
    @Override
    public int batchDelete${entity.entityName}(${entity.entityName}SearchPo searchPo) {

        List<${entity.entityName}Bean> ${entity.entityNameFirstLower}s = ${entity.entityNameFirstLower}Mapper.search${entity.entityName}(searchPo, PageableUtils.toPageableMyBatis(GPageRequest.all()));
        List<String> ${entity.entityNameFirstLower}Keys = ${entity.entityNameFirstLower}s.stream().map(${entity.entityName}Bean::get${entity.entityName}Key).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(${entity.entityNameFirstLower}Keys)){
            ${entity.entityNameFirstLower}Mapper.deleteBatchIds(${entity.entityNameFirstLower}Keys);
        }
        return BaseResultCodes.OK.getValue();
    }

    /**
     * 添加${entity.entityLabel}信息
     * @param ${entity.entityNameFirstLower}Po ${entity.entityLabel}表单对象
     * @return
     */
    @GAction("add${entity.entityName}")
    @GDataChanged("search${entity.entityName}")
    @Override
    public ${entity.entityName}Bean add${entity.entityName}(${entity.entityName}Po ${entity.entityNameFirstLower}Po) {

        ${entity.entityName}Bean ${entity.entityNameFirstLower} = new ${entity.entityName}Bean();
        BeanUtil.copyProperties(${entity.entityNameFirstLower}Po, ${entity.entityNameFirstLower}, CopyOptions.create().setIgnoreNullValue(true));
        ${entity.entityNameFirstLower}.set${entity.entityName}Key(KeyOnlyUtils.general("${entity.entityNameFirstLower}"));
        ${entity.entityNameFirstLower}Mapper.insert(${entity.entityNameFirstLower});
        
        return ${entity.entityNameFirstLower};
    }

    /**
     * 修改${entity.entityLabel}
     * @param ${entity.entityNameFirstLower}Key ${entity.entityLabel}主键KEY
     * @param ${entity.entityNameFirstLower}Po  ${entity.entityLabel}表单对象
     * @return
     */
    @GAction("update${entity.entityName}")
    @GDataChanged("search${entity.entityName}")
    @Override
    public ${entity.entityName}Bean update${entity.entityName}(String ${entity.entityNameFirstLower}Key, ${entity.entityName}Po ${entity.entityNameFirstLower}Po) {
//        if (meetingCenterService.is${entity.entityName}Started(${entity.entityNameFirstLower}Key)) {
//            throw new MyException(MeetingResultCode.${entity.entityNameFullUpperCase}_IS_RUNNING.getValue(), "${entity.entityLabel}正在运行");
//        }

        ${entity.entityName}Bean ${entity.entityNameFirstLower}Bean = ${entity.entityNameFirstLower}Mapper.selectById(${entity.entityNameFirstLower}Key);
        if (${entity.entityNameFirstLower}Bean == null) {
            throw new MyException(MeetingResultCode.${entity.entityNameFullUpperCase}_NOT_FOUND.getValue(), "根据${entity.entityNameFirstLower}Key找不到数据");
        }

        BeanUtil.copyProperties(${entity.entityNameFirstLower}Po, ${entity.entityNameFirstLower}Bean, CopyOptions.create().setIgnoreCase(true).setIgnoreNullValue(true).setIgnoreError(true));
        ${entity.entityNameFirstLower}Mapper.updateById(${entity.entityNameFirstLower}Bean);
        return ${entity.entityNameFirstLower}Bean;
    }

}
